package ngram_reader.validator;

import java.io.File;
import java.io.DataInputStream;
import java.io.EOFException;
import main.errorUpdate.ErrorUpdate;
import ngram.reader.V1FileReader;
import swingExtended.progressPanel.ProgressPanel;

/**
 * Validator for Version 1 File Format.
 * @author Shimu
 * @date 9-Nov-2013
 */
public class Validator_Version1 extends Validator {

    public Validator_Version1(ProgressPanel panel, File dir,
            boolean deleteCorrupted, boolean validateAllFiles) {
        super(panel, dir, deleteCorrupted, validateAllFiles);
    }

    @Override
    protected boolean validate(DataInputStream stream) throws Exception {
        V1FileReader reader = new V1FileReader(stream);
        int nGramCount = 0;
        while (!isCancelled() && (reader.readNGram()) != null) {
            nGramCount++;
            
            // Read a header that tells the number of year, count information
            // that follows
            int count = reader.readHeader();

            try {
                reader.skip(count);

            } catch (EOFException ex) {
                publish(new ErrorUpdate(PANEL, "Invalid line: " + nGramCount, ex));
                return false;
            }
        }
        return true;
    }
}
